package whu.cn.whushare.ui.main.known;

import java.util.ArrayList;
import java.util.List;

import whu.cn.whushare.R;
import whu.cn.whushare.common.Constants;
import whu.cn.whushare.net.DownloadParams;
import whu.cn.whushare.net.Downloader;
import whu.cn.whushare.ui.common.BaseActivity;
import whu.cn.whushare.ui.common.LoadingHelper;
import whu.cn.whushare.ui.main.known.adapter.SearchQuestionAdapter;
import whu.cn.whushare.ui.main.known.bean.SearchQuestion;
import whu.cn.whushare.util.Utils;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;

import com.android.volley.VolleyError;
import com.androidplus.util.ViewMapping;
import com.androidplus.util.ViewMappingUtil;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

/**
 * 类 名：SearchQuestionListActivity
 * 功 能：主要完成问题搜索操作
 */
@ViewMapping(id = R.layout.activity_known_question_list)
public class SearchQuestionListActivity extends BaseActivity {
	private LoadingHelper mLoadingHelper;
	private String mQuestion;
	private int mCurrentPage = 1;

	@ViewMapping(id = R.id.question_list)
	private PullToRefreshListView mRefreshListView;
	private List<SearchQuestion> mQuestions = new ArrayList<>();

	public static Intent getStartIntent(Context context, String question) {
		Intent i = new Intent(context, SearchQuestionListActivity.class);
		i.putExtra(Constants.INTENT_EXTRA_QUESTION, question);
		i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
		return i;
	}

    /**
     * 创建一个请求搜索问题的网络请求
     * @param query - 搜索字符串
     * @param page - 当前页面
     * @param pageSize - 每页显示问题数目
     */
	protected void requestSearchQuestion(String query, int page, int pageSize) {
		Downloader.getInstance(this).getQueryQuestionsList(query, page, pageSize, this, page);
		mLoadingHelper.showLoadingView(page == 1);      //当当前页面不为1时显示正在加载的页面视图
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getIntentData();
		initView();
		requestSearchQuestion(mQuestion, 1, 10);
	}

    //从跳转到当前activity的intent中获取搜索字符串信息
	private void getIntentData() {
		if (getIntent() != null) {
			mQuestion = getIntent().getStringExtra(Constants.INTENT_EXTRA_QUESTION);
		}
	}

    //初始化页面
	private void initView() {
		View root = ViewMappingUtil.mapView(this, this);    //将当前activity与其布局一一映射
		getTopBar(root);                                    //获取顶部菜单条
		mTopBar.searchLayout.setVisibility(View.VISIBLE);   //将顶部菜单条的搜索视图设为可见
		initLoadingHelper();
		initListView();
		requestSearchQuestion(mQuestion, 1, 10);
	}

	private void initListView() {
		SearchQuestionAdapter adapter = new SearchQuestionAdapter(this, mQuestions);
		mRefreshListView.setAdapter(adapter);
		mRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
			@Override
            //下拉刷新操作
			public void onPullDownToRefresh(PullToRefreshBase<ListView> listViewPullToRefreshBase) {
				requestSearchQuestion(mQuestion, 1, 10);
				mRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);  //设置listview为上拉更新和下拉更新两种模式都可以
				Utils.resetRefreshLabel(SearchQuestionListActivity.this, mRefreshListView); //重置刷新标志
			}

			@Override
            //上拉刷新操作
			public void onPullUpToRefresh(PullToRefreshBase<ListView> listViewPullToRefreshBase) {
				if (mQuestions.size() >= mTotal) {
					Utils.setRefreshLabelToLast(SearchQuestionListActivity.this, mRefreshListView);
					Utils.COMMON_HANDLER.sendMessageDelayed(
							Message.obtain(Utils.COMMON_HANDLER, Utils.REFRESH_TO_COMPLETE, mRefreshListView), 500);
				} else {
					requestSearchQuestion(mQuestion, mCurrentPage + 1, 10);
				}
			}
		});
	}

	private void initLoadingHelper() {
		mLoadingHelper = new LoadingHelper(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

			}
		});
		mLoadingHelper.onCreateView(LayoutInflater.from(this), mRefreshListView);
	}

	private void updateView(List<SearchQuestion> searchQuestions) {
		if (searchQuestions != null && searchQuestions.size() > 0) {
			mQuestions.addAll(searchQuestions);
		}
	}

	@Override
    /**
     * 搜索问题成功时调用该方法
     * @param type - URL类型
     * @param response - 返回结果信息
     * @param obj -
     * @see whu.cn.whushare.net.NetResponseListener#onSuccess(whu.cn.whushare.net.DownloadParams.PARSING_TYPE, whu.cn.whushare.bean.Result<T>, java.lang.Object)
     */
	public <T> void onSuccess(DownloadParams.PARSING_TYPE type, T response, Object obj) {
		super.onSuccess(type, response, obj);
		mCurrentPage = (int) obj;
		mLoadingHelper.showContentView();
		updateView((List<SearchQuestion>) response);
	}

	@Override
    /**
     * 搜索问题失败时调用该方法
     * @param type - URL类型
     * @param response - 返回结果信息
     * @param error -
     * @see whu.cn.whushare.net.NetResponseListener#onFail(whu.cn.whushare.net.DownloadParams.PARSING_TYPE, whu.cn.whushare.bean.Result, com.android.volley.VolleyError)
     */
	public void onFail(DownloadParams.PARSING_TYPE type, VolleyError error) {
		super.onFail(type, error);
		if (mCurrentPage <= 1) {
			mLoadingHelper.showRetryView();
		}
	}
}
